# -*- coding: utf-8 -*-
'''
Created on 2020年5月9日

@author: Kitch
'''
from sqlalchemy import distinct, func, desc

try:
    from ..models import *
except:
    from models import *


async def add_edit(id: int = 0, name: str = "", notes: str = ""):
    """ insert into the database if the id does NOT exist
    or update the record
    """
    try:
        e = await get(id)
        if not e:
            e = Example()
            session.add(e)

        e.name = name
        e.notes = notes
        session.commit()
        return e.id
    except:
        session.rollback()
        raise
    
    
async def delete(id: int = 0):
    """ delete the record
    """
    try:
        session.query(Example).filter(Example.id == id).delete()
    except:
        session.rollback()
        raise


async def get(id: int = None):
    """ return the object if the id exist
    or return the object list
    """
    if id is not None:
        return session.query(Example).filter(Example.id == id).first()
    else:
        return session.query(Example).all()
